package com.iyoudoock.citymine.activity.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.bean.AroundPeopleBean;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;
import com.kvin.toolkit.widget.KvinWebView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PersonCenterActivity extends BaseActivity {

    @BindView(R.id.user_avatar_iv)
    ImageView userAvatarIv;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.phone_tv)
    TextView phoneTv;
    @BindView(R.id.age_tv)
    TextView ageTv;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.exchange_num_tv)
    TextView exchangeNumTv;
    @BindView(R.id.exchange_record_tv)
    TextView exchangeRecordTv;
    @BindView(R.id.null_data_container)
    LinearLayout nullDataContainer;
    @BindView(R.id.record_wv)
    KvinWebView recordWv;

    private AroundPeopleBean bean;
    private String nickName;
    private String phone;
    private String age;
    private String address;
    private String userAvatar;
    private String targetUid;

    private boolean isAdded = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_center);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        setTitle("个人中心");
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();
        //set content
        bean = getIntent().getParcelableExtra(Key.Tag.PEOPLE);
        if (bean != null) {
            nickName = bean.getUsername();
            phone = bean.getTelephone();
            age = bean.getAge();
            address = bean.getAddress();
            userAvatar = bean.getHeadImage();
            targetUid = String.valueOf(bean.getUid());
        } else {
            nickName = mPersistentUtils.readString(Key.User.NICK_NAME);
            phone = mPersistentUtils.readString(Key.User.TELEPHONE);
            age = mPersistentUtils.readString(Key.User.AGE);
            address = mPersistentUtils.readString(Key.User.ADDRESS);
            userAvatar = mPersistentUtils.readString(Key.User.AVATAR);
        }

        if (!TextUtils.isEmpty(userAvatar)) {
            ImageLoader.getInstance().displayImage(userAvatar, userAvatarIv);
        }
        this.nameTv.setText(nickName);
        this.phoneTv.setText(phone);
        this.ageTv.setText(age);
        this.addressTv.setText(address);
    }


    @Override
    public void onNavNextIcon(Menu menu) {
        if (menu == null) return;//just for safe
        mMenu = menu;
        int from = getIntent().getIntExtra(Key.Tag.FROM, -1);
        if (from != Value.Act.MAP) return;

        menu.add(0, 0, 0, "")
                .setIcon(R.mipmap.add_to_blacklist)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    }

    @Override
    public void onNextClick(MenuItem menuItem) {
        if (menuItem == null) return;
        if (!isAdded) {
            addToBlacklist(targetUid);
        } else {
            removeFromBlacklist(targetUid);
        }
    }

    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            int state = response.getInt(Key.Tag.STATE);
            if (flag.equals(Value.Flag.ADD_TO_BLACKLIST) && state == Value.Response.SUCCESS) {
                isAdded = true;
                mMenu.clear();
                mMenu.add(0, 0, 0, "").setIcon(R.mipmap.remove_from_blacklist).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
                String msg = response.getString(Key.Tag.MSG);
                ToastUtils.simpleToast(getActivity(), msg);
            } else if (flag.equals(Value.Flag.REMOVE_FROM_BLACKLIST) && state == Value.Response.SUCCESS) {
                isAdded = false;
                mMenu.clear();
                mMenu.add(0, 0, 0, "").setIcon(R.mipmap.add_to_blacklist).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
                String msg = response.getString(Key.Tag.MSG);
                ToastUtils.simpleToast(getActivity(), msg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }
}
